<?php

/**
 * Table user name
 *
 * @author NguyenVanTien
 */
class Application_Model_DbTable_UserFriends extends Zend_Db_Table_Abstract {

    protected $_name = 'user_friends';
    protected $_primary = 'user_friend_id';

    /**
     * Get list friends
     * @author Tien Nguyen
     * @param string $username
     * @param string $password 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function getFriends($username) {
        $select = $this->select()
                ->from('user_friends')
                ->setIntegrityCheck(FALSE)
                ->join(array('u1' => 'users'), 'u1.user_id = user_friends.user_id1', array())
                ->join(array('u2' => 'users'), 'user_friends.user_id2 = u2.user_id')
                ->where('u1.username = ?', $username);
        return $this->fetchAll($select);
    }

    /**
     * Add friend for user
     * @author Tien Nguyen
     * @param int $userId
     * @param int $friendId 
     * @return int
     */
    public function addFriend($userId, $friendId) {
        $data = array('user_id1' => $userId, 'user_id2' => $friendId);
        return $this->createRow($data)->save();
    }

    /**
     * Remove friend
     * @author Tien Nguyen
     * @param int $userId
     * @param int $friendId 
     * @return boolean
     */
    public function removeFriend($userId, $friendId) {
        $rs = false;
        try {
            $rs = $this->delete('user_id1 = ' .
                    $this->getAdapter()->quote($userId) .
                    ' and user_id2 = ' . $this->getAdapter()->quote($friendId));
            $rs = true;
        } catch (Exception $e) {
            $rs = false;
        }
        return $rs;
    }

}

?>
